//***********************************************************************************
// Function to apply level 2 rule set. Both movements meaning, level3-level2 and level2-level1 have been encapsulated.

// Author: Aravindh U095232L

//***********************************************************************************

//Function to spawn new NPCs at level 2 if needed, y decompressing level3
function level_2_rules()
{
	for(var i =0;i<level_2.length;i++)
	{		
		if(level_2[i].isEmpty()==false)
			level_2[i].rendered_2 = true;

		if(level_2[i].refresh_timer>500)
		{	level_2[i].rendered_2 = false;
			level_2[i].removeAllNPCs();
			level_2[i].refresh_timer=0;
		}

		level_2[i].refresh_timer++;

		//console.log(level_2[i].refresh_timer);
		if(level_2[i].rendered_2)
			rule_system_2(level_2[i]);

		obstacle_map = level_2[i].getObstacleMap();

		buildings = level_2[i].getBuildings();

		//  if(buildings.length)
		// console.log(buildings[0].obj.getType());
  
	    building_type = 0;

	    if(buildings.length)
	    building_type = buildings[0].obj.getType();

		var no_of_army = 0;
		var no_of_rebel = 0;
		var no_of_spy = 0;
		var no_of_merchant = 0;
		var no_of_citizen =0;
		var total_people = 0;

		var no_of_army_building = 0;
		var no_of_rebel_building = 0;
		var no_of_spy_building = 0;
		var no_of_merchant_building = 0;
		var no_of_citizen_building =0;
		var total_people_building = 0;

	    if(building_type == "inArmyHQ" && level_2[i].rendered_2 == false)
		{	
			 total_people = Math.round(Math.random()*2+2);

			 if (Math.random() < 0.6 && army_fight > 1)
			 {
			 	no_of_army = Math.round(Math.random()*1+1);
			 }
			 else
			 	no_of_army = 1;
				
			 if(Math.random() < 0.35)
			 {
			 	no_of_spy = Math.round(Math.random()*2);
			 }	
			 else
			 	no_of_spy = Math.round(Math.random());

			 if (Math.random() < 0.6 && army_fight > 1)
			 {
			 	no_of_rebel = Math.round(Math.random()*2);
			 }
			 else
			 	no_of_rebel = Math.round(Math.random());

			 no_of_citizen = total_people - no_of_army - no_of_spy - no_of_rebel;

			 if(no_of_citizen<0)
			 	no_of_citizen = 0;

			 total_people_building = Math.round(Math.random()*1+1); 

			 if(Math.random()<0.5)
			 	no_of_army_building = Math.round(total_people_building/2);
			 else 
			 	no_of_army_building = 1;
			 
			 no_of_spy_building = total_people_building - no_of_army_building;

			 if(no_of_spy_building<0)
			 	no_of_spy_building =0;
		}
		
		else if(building_type == "inTvStation" && level_2[i].rendered_2 == false)
		{	
			total_people = Math.round(Math.random()*2+1);

			var rebel_ratio = rebel_control/(rebel_control + army_control);
			var army_ratio = army_control/(rebel_control + army_control);

			var idle_ratio = 0.8;
			var curr_control = 0;
			var random = Math.random() ;
			if (random <= (0.8*rebel_ratio))
			{
				curr_control = 1;
			} 
			else if( (random > (0.8*rebel_ratio)) && (random <0.8) )
				curr_control = 2; 

			if(curr_control == 1)
			{
				no_of_rebel = Math.round(Math.random()*1);
			}
			else if(curr_control == 2)
			{	
				no_of_army = Math.round(Math.random()*1);
				no_of_spy = Math.round(Math.random()*1);
			}

			no_of_citizen = total_people - no_of_rebel - no_of_army - no_of_spy;

			if(no_of_citizen<0)
			 	no_of_citizen = 0;

			 total_people_building = Math.round(Math.random()*1+1); 

			 if(curr_control ==1)
			 	no_of_rebel_building = total_people_building;
			 else if(curr_control == 2)
			 	no_of_army_building = total_people_building;
		}

		else if(building_type == "inRebelHQ" && level_2[i].rendered_2 == false)
		{
			 total_people = Math.round(Math.random()*2+1);

			 if (Math.random() < 0.4 && rebel_fight > 1)
			 {
			 	no_of_rebel = Math.round(Math.random()*1+1);
			 }
			 else if(Math.random() < 0.4 && rebel_fight > 1)
			 {
			 	no_of_rebel = Math.round(Math.random()*2);
			 }
			 else
			 	no_of_rebel = 1;

			 if (Math.random() < 0.4 && rebel_fight > 1)
			 {
			 	no_of_army = Math.round(Math.random());
			 }
			 else
			 	no_of_army = Math.round(Math.random());

			 if(( for_war/against_war) > 1)
				if(Math.random()>0.6)
					no_of_spy = 1;

			 no_of_citizen = total_people - no_of_army - no_of_spy - no_of_rebel;

			 if(no_of_citizen<0)
			 	no_of_citizen = 0;

			 total_people_building = Math.round(Math.random()*1+1); 

			 if(Math.random()<0.5)
			 	no_of_rebel_building = Math.round(total_people_building/2);
			 else 
			 	no_of_rebel_building = 1;

			 no_of_citizen_building = total_people_building - no_of_rebel_building;

			 if(no_of_citizen_building<0)
			 	no_of_citizen_building =0;

		}
		else if(building_type == "inBlackMarket" && level_2[i].rendered_2 == false)
		{
			total_people_building = Math.round(Math.random()*2+1);

			no_of_merchant = Math.round(Math.random()*1+1);

			if(Math.round(Math.random()<0.5))
				no_of_army = Math.round(Math.random());
			else
			    no_of_rebel = Math.round(Math.random());
			
			no_of_rebel = total_people - no_of_army - no_of_merchant;  

			if(no_of_rebel<0)
				no_of_rebel = 0; 

			 total_people_building = Math.round(Math.random()*1+1); 
			 Math.round(Math.random()<0.7)
			 	no_of_merchant = 1;
			 if(total_people_building==2)
			 	no_of_rebel = 1;		   
		}
		else //Street
		{
			total_people = 4;//Math.round(Math.random()	*2+2);

			var rebel_ratio = against_war/(for_war+against_war);
			var army_ratio = for_war/(for_war+against_war);
			var roadshow = 0;

			if(show_ratio > 1)
			{
				no_of_rebel = Math.round(Math.random()*1+1);
			}
			else
				no_of_rebel = Math.round(Math.random());

			if(Math.random()>rebel_ratio)
				no_of_spy = Math.round(Math.random()*1);
			else
			{
				no_of_army = Math.round(Math.random()+1);
				no_of_spy = Math.round(Math.random());
			}

			if(Math.random()>0.5)
			{
				if(total_people - no_of_army - no_of_spy - no_of_rebel)
					no_of_merchant = 1;
			}

			 no_of_citizen = total_people - no_of_army - no_of_spy - no_of_rebel-no_of_merchant;

			 if(no_of_citizen<0)
			 	no_of_citizen = 0;

		}

		//Rendering everything as per calculations

		if(level_2[i].rendered_2 == false)
		{

		//console.log ( total_people, no_of_army, no_of_rebel, no_of_spy , no_of_merchant ,no_of_citizen);

		//console.log ( total_people_building, no_of_army_building, no_of_rebel_building, no_of_spy_building , no_of_merchant_building ,no_of_citizen_building);

		for(var j =0; j< no_of_army  ;j++)
			 create(level_2[i],"Army");

		for(var j=0;j< no_of_rebel;j++)
			create(level_2[i],"Rebel");

		for(var j =0; j<no_of_citizen  ;j++)
		 	create(level_2[i],"Citizen");

		for(var j=0;j<no_of_merchant;j++)
			create(level_2[i],"Merchant");

		for(var j=0;j<no_of_spy;j++)
			create(level_2[i],"Spy");

		for(var j =0; j< no_of_army_building  ;j++)
			 create_building(level_2[i],"Army");

		for(var j=0;j< no_of_rebel_building;j++)
			create_building(level_2[i],"Rebel");

		for(var j =0; j<no_of_citizen_building  ;j++)
		 	create_building(level_2[i],"Citizen");

		for(var j=0;j<no_of_merchant_building;j++)
			create_building(level_2[i],"Merchant");

		for(var j=0;j<no_of_spy_building;j++)
			create_building(level_2[i],"Spy");

		level_2[i].rendered_2 = true;
		}

	}

}


//Function to apply level_2 rule sets
function rule_system_2(curr_grid)
{

	var NPC = curr_grid.getNPCs();
	var army = NPC.Army;
	var rebel = NPC.Rebel;
	var citizen = NPC.Citizen;
	var spy = NPC.Spy;
	var merchant = NPC.Merchant;

	arrayOfSurroundingObjects = curr_grid;

	if(Math.round(Math.random()<0.01))
	{
		var random_key = Math.round(Math.random()*5.5+0.5); 

		switch (random_key)
		{
			case 1: if(merchant && rebel)
					{	
						if(merchant.length!=0 && rebel.length!=0)
						{	console.log("sold");
			       		 	Interactions.determineTypeOfEvent(merchant[0],rebel[0]);}
			       		 	//console.log(rebel[0].eventOutcomeQueue);
					}
					break;
			case 2: if(rebel && spy && citizen)
					{
						if (Math.random()<0.5)
						{	if(spy.length!=0 && citizen.length!=0)
							{	if(citizen[0].eventType=="default")
									Interactions.determineTypeOfEvent(citizen[0],spy[0]);
							}
						}
						else
						{	if(rebel.length!=0 && citizen.length!=0)
							{	if(citizen[0].eventType=="default")
									Interactions.determineTypeOfEvent(citizen[0],rebel[0]);
							}
						}
					}
					break;

			case 3: if (army && rebel)
					{
						if(army.length!=0 && rebel.length!=0)
							Interactions.determineTypeOfEvent(army[0],rebel[0]);
					}
					break;
			case 4: if	(spy && citizen)
					{	
						if(spy.length!=0 && citizen.length!=0)
							if(citizen[0].eventType=="default")
								Interactions.determineTypeOfEvent(spy[0],citizen[0]);	
					}
					break;
			case 5: if (rebel && citizen)
					{
						if(rebel.length!=0 && citizen.length!=0)
							if(citizen[0].eventType=="default")
								Interactions.determineTypeOfEvent(citizen[0],rebel[0]);	
					}
					break;
			case 6: if (citizen)
					{	console.log("cit");
						if(citizen.length>=2)
							if(citizen[0].eventType=="default" && citizen[1].eventType=="default"  )
								Interactions.determineTypeOfEvent(citizen[0],citizen[1]);	
					}
		}

		//console.log(random_key, curr_grid.getIndexOfGrid());
	}

}
